package cn.whitecao.agent.plugin.jdbc;

import cn.whitecao.agent.core.plugin.PluginMatcherRule;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;

import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.List;

/**
 * 切servlet的规则
 *
 * @author : BashfulHuang
 **/

public class JdbcMatcherRule implements PluginMatcherRule {
    /**
     * 匹配类型
     *
     * @return ElementMatcher<TypeDescription>
     */
    @Override
    public ElementMatcher<TypeDescription> getTypesMatcher() {

        return ElementMatchers
                .not(ElementMatchers.<TypeDescription>isInterface())
                .and(ElementMatchers.not(ElementMatchers.<TypeDescription>isAbstract()))
                .and(ElementMatchers.isSubTypeOf(Statement.class))
                //当前用的mysql8高版本的mysql包名加了 cj的 不带cj的后面要测试一下版本都要测试一下
                .and(ElementMatchers.<TypeDescription>nameStartsWith("com.mysql"));
    }

    /**
     * 匹配方法
     *
     * @return ElementMatcher<MethodDescription>
     */
    @Override
    public ElementMatcher<MethodDescription> getMethodsMatcher() {
        //public的execute开头的方法
        return ElementMatchers.isMethod()
                .and(ElementMatchers.<MethodDescription>nameStartsWith("execute"))
                .and(ElementMatchers.<MethodDescription>isPublic())
                .and(ElementMatchers.not(ElementMatchers.<MethodDescription>named("executeInternal")));
    }


}
